package project;

import java.io.IOException;
import java.util.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

	public class AnimalAnimate extends GameCanvas implements Runnable
	{
		private AnimalMain midlet;
	 	private Sprite AnimalSprite;
		private LayerManager layerManager;
		//private ObstacleManager obsManager;
		private int MAX_OWLS = 3;
		private boolean gameRunning;
		private boolean collision = false;
//		private Sprite [] owl;
    	private int width;
    	private int height;
		private long gameDuration;
		List action;
		
		public AnimalAnimate(AnimalMain hdmidlet,String animalImageName)throws IOException 
		{
	  		super(true);
			this.midlet = hdmidlet;  //used later
			layerManager = new LayerManager();
			width = getWidth(); 
	     	height = getHeight();

			layerManager.setViewWindow(1,1,width - 2, height -2);
			createAnimal(animalImageName,7); //this number sets
			//how many animals are on screen
			//obsManager = new ObstacleManager( this, layerManager, animalImageName);
			
		} 

		private void createAnimal(String animalImageName, int number) throws IOException 
		{
			Image AnimalImage = Image.createImage(animalImageName);
			Vector animals = new Vector();
			AnimalSprite = new Sprite(AnimalImage);
			for(int i = 0; i < number; i++){
				animals.addElement(new Sprite(AnimalImage));
				//animals.elementAt(i).setVisible(false);
			
				layerManager.append((Sprite)animals.elementAt(i));	
			}
			// Put the sprite close to bottom center
			AnimalSprite.setPosition(width/2,height - 30);
			layerManager.append(AnimalSprite);
		}

		public void start() 
		{
	  		action = new List("Does this work?",Choice.IMPLICIT);
	        //menu.append("Test TextBox", null);
	        action.append("Play Game", null);
	        action.append("Instructions", null);
	        action.append("About", null);
			gameRunning = true;
			Thread gameThread = new Thread(this);
			gameThread.start();
		}

		public void stop() { gameRunning = false; }

		public void run() 
		{
			Graphics g = getGraphics();
			int timeStep = 20;
			//obsManager.renderObstacles();
			long startTime = System.currentTimeMillis();
			while (gameRunning) //is true                                    
			{
				input();
				render(g);
				
				long endTime = System.currentTimeMillis();
				long duration = (int)(endTime - startTime);
				gameDuration = duration / 1000;  //game time in seconds

				try
				{
					Thread.sleep(timeStep);//- duration); 
				//	obsManager.MoveObstacles();
				}
	        		catch (InterruptedException ie) { stop(); }

				
			}

		}			

		private void input() 
		{    
			int keyStates = getKeyStates();
			int currentY = AnimalSprite.getY();
			int currentX = AnimalSprite.getX( );
			if ((keyStates & LEFT_PRESSED) != 0)      
			AnimalSprite.setPosition (Math.max(0, currentX-5), currentY);    
			if ((keyStates & RIGHT_PRESSED) != 0)      
			AnimalSprite.setPosition(Math.min(170, currentX + 5), currentY);
			if ((keyStates & UP_PRESSED) != 0)      
				AnimalSprite.setPosition(Math.min(170, currentX), currentY-5);
			if ((keyStates & DOWN_PRESSED) != 0)      
				AnimalSprite.setPosition(Math.min(170, currentX), currentY+5);

		}
		private void render(Graphics g) 
		{
			g.setColor(255, 25,3); // white
	  		//Display.drawImage(image, 34, 56, 45);
			g.fillRect(0, 0,getWidth(), getHeight());
	  		
			layerManager.paint(g,0,0);
			flushGraphics();

		}
	}
